//
//  RVChangeplanViewController.m
//  RememberVocabularies
//
//  Created by 伍银荣 on 2017/10/15.
//  Copyright © 2017年 WYR. All rights reserved.
//

#import "RVChangeplanViewController.h"
#import "RVChangePlanCollectionViewCell.h"
#import "bookModel.h"

@interface RVChangeplanViewController ()<UICollectionViewDelegateFlowLayout,UICollectionViewDelegate,UICollectionViewDataSource,UIPickerViewDataSource,UIPickerViewDelegate>

@property (nonatomic,strong) UICollectionView *collectionView;
//存放书籍的数组
@property (nonatomic,strong) NSArray *bookArr;

//选择书籍的label
@property(nonatomic, strong) UILabel *selectBookLabel;

//修改计划的label
@property (nonatomic,strong) UILabel *changeplanLabel;

//pickerView
@property (nonatomic,strong) UIPickerView *pickerView;

//完成天数的label
@property (nonatomic,strong) UILabel *finishedDaysLabel;

//每天背单词个数的label
@property (nonatomic,strong) UILabel *wordsNumLabel;

//创建一个存放天数的数组
@property (nonatomic,strong) NSArray *daysArr;

//创建一个存放单词个数的数组
@property (nonatomic,strong) NSArray *wordsArr;

//取值属性
//1.天数
@property (nonatomic, copy) NSString *dayStr;
//2.词数
@property (nonatomic, copy) NSString *wordStr;
//3.书名
@property (nonatomic, copy) NSString *bookName;
//书本路径
@property (nonatomic, copy) NSString *bookPath;
//4.书词数
@property (nonatomic, copy) NSString *bookWordNum;


@property(nonatomic, strong) NSIndexPath *selectIndex;

@end
static NSString *ID = @"cellId";

@implementation RVChangeplanViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
}

-(void)setupUI
{
    self.navigationItem.title = @"我的计划";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"确定" style:UIBarButtonItemStyleDone target:self action:@selector(certainBtnItemClick)];
    
    UIView *topView = [[UIView alloc]initWithFrame:CGRectMake(0, 64, Width, 180)];
    topView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [topView addSubview:self.selectBookLabel];
    [topView addSubview:self.collectionView];
    [self.view addSubview:topView];
    
    
    UIView *bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, 244, Width, Height - 244)];
    bottomView.backgroundColor = RVColor(218.0, 213.0, 194.0);
    [bottomView addSubview:self.changeplanLabel];
    [bottomView addSubview:self.pickerView];
    [self.view addSubview:bottomView];
    
    [bottomView addSubview:self.finishedDaysLabel];
    [bottomView addSubview:self.wordsNumLabel];
    
    NSMutableArray *daysMarr = [NSMutableArray new];
    for (int i = 0; i <= 100; i += 2) {
        NSString *day = [NSString stringWithFormat:@"%d 天",i + 2];
        [daysMarr addObject:day];
    }
    self.daysArr = daysMarr;
    
    
    NSMutableArray *wordsMarr = [NSMutableArray new];
    for (int i = 0; i <= 200; i += 5) {
        NSString *words = [NSString stringWithFormat:@"%d 个单词",i + 5];
        [wordsMarr addObject:words];
    }
    self.wordsArr = wordsMarr;
    [self.pickerView reloadAllComponents];
}

-(UILabel *)finishedDaysLabel
{
    if (!_finishedDaysLabel) {
        UILabel *finishedDaysLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 80, Width/2, 30)];
        finishedDaysLabel.text = @"完成天数";
        finishedDaysLabel.font = [UIFont systemFontOfSize:18];
        finishedDaysLabel.textColor = [UIColor grayColor];
        finishedDaysLabel.textAlignment = NSTextAlignmentCenter;
        _finishedDaysLabel = finishedDaysLabel;
    }
    return _finishedDaysLabel;
}

-(UILabel *)wordsNumLabel
{
    if (!_wordsNumLabel) {
        UILabel *wordsNumLabel = [[UILabel alloc]initWithFrame:CGRectMake(Width/2, 80, Width/2, 30)];
        wordsNumLabel.text = @"每天背单词";
        wordsNumLabel.font = [UIFont systemFontOfSize:18];
        wordsNumLabel.textColor = [UIColor grayColor];
        wordsNumLabel.textAlignment = NSTextAlignmentCenter;
        _wordsNumLabel = wordsNumLabel;
    }
    return _wordsNumLabel;
}


-(UILabel *)selectBookLabel
{
    if (!_selectBookLabel) {
        UILabel *selectBookLabel = [[UILabel alloc]initWithFrame:CGRectMake(12, 10, 100, 30)];
        selectBookLabel.text = @"书籍选择";
        selectBookLabel.font = [UIFont systemFontOfSize:20];
        selectBookLabel.textColor = MRGlobalBg;
        _selectBookLabel = selectBookLabel;
    }
    return _selectBookLabel;
}

-(UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = CGSizeMake(90, 110);
        layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        UICollectionView *myCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 40, Width, 140) collectionViewLayout:layout];
        [myCollectionView registerNib:[UINib nibWithNibName:NSStringFromClass([RVChangePlanCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:ID];
        myCollectionView.delegate = self;
        myCollectionView.dataSource = self;
        myCollectionView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        myCollectionView.showsHorizontalScrollIndicator = NO;
        _collectionView = myCollectionView;
    }
    return _collectionView;
}

-(NSArray *)bookArr
{
    if (!_bookArr) {
        //获取书籍文件路径
        NSString *bookPath = [[NSBundle mainBundle]pathForResource:@"TABLE_META.json" ofType:nil];
        //读取文件路径
        NSData *bookData = [NSData dataWithContentsOfFile:bookPath];
        NSArray *arr = [NSJSONSerialization JSONObjectWithData:bookData options:NSJSONReadingAllowFragments error:nil];
        NSMutableArray *bookMarr = [NSMutableArray array];
        //用字典对象去遍历数组
        for (NSDictionary *dict in arr) {
            bookModel *bookItem = [[bookModel alloc]initWithDict:dict];
            [bookMarr addObject:bookItem];
        }
        _bookArr = bookMarr;
        NSLog(@"%@:********bookArr",_bookArr);
    }
    return _bookArr;
}

-(UILabel *)changeplanLabel
{
    if (!_changeplanLabel) {
        UILabel *changeplanLabel = [[UILabel alloc]initWithFrame:CGRectMake(12, 30, 100, 30)];
        changeplanLabel.text = @"修改计划";
        changeplanLabel.font = [UIFont systemFontOfSize:20];
        changeplanLabel.textColor = MRGlobalBg;
        _changeplanLabel = changeplanLabel;
    }
    return _changeplanLabel;
}

-(UIPickerView *)pickerView
{
    if (!_pickerView) {
        UIPickerView *pickerView = [[UIPickerView alloc]init];
        pickerView.frame = CGRectMake(0, 110, Width, 300);
        pickerView.delegate = self;
        pickerView.dataSource = self;
        [pickerView selectRow:0 inComponent:0 animated:YES];
        [pickerView selectRow:0 inComponent:1 animated:YES];
        _pickerView = pickerView;
    }
    return _pickerView;
}

-(NSArray *)daysArr
{
    if (!_daysArr) {
        _daysArr = [NSArray array];
    }
    return _daysArr;
}

-(NSArray *)wordsArr
{
    if (!_daysArr) {
        _wordsArr = [NSArray array];
    }
    return _wordsArr;
}


//确定按钮的返回事件
-(void)certainBtnItemClick
{
    NSDictionary *dict = @{@"day":self.dayStr,@"wordNum":self.wordStr,@"bookName":self.bookName,@"bookWordNum":self.bookWordNum,@"bookPath":self.bookPath};
        if ([self.delegate respondsToSelector:@selector(backValue:)]) {
        [self.delegate backValue:dict];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.bookArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    RVChangePlanCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    cell.bookNameLabel.text = [self.bookArr[indexPath.row] Meta_Name];
    cell.wordNumLabel.text = [self.bookArr[indexPath.row] Meta_WordCount];
    if (self.selectIndex == indexPath) {
        cell.confirmImg.hidden = NO;
    } else {
        cell.confirmImg.hidden = YES;
    }
    return cell;
}

#pragma mark - <UIPickerViewDelagate>

//获取pickerView指定列中包含的列表项的数量（该属性是一个只读属性）
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

//每一列的高度
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 44;
}

//获取一列包含的列表项
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == 0) {
        return self.daysArr.count;
    }else{
        return self.wordsArr.count;
    }
}

//列标题
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component == 0) {
        return [self.daysArr objectAtIndex:row];
    }else{
        return [self.wordsArr objectAtIndex:row];
    }
}

//指定列和列表项时激发的方法
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component == 0) {
        self.dayStr = [[self.daysArr[row] componentsSeparatedByString:@" "] firstObject];
    } else {
        self.wordStr = [[self.wordsArr[row] componentsSeparatedByString:@" "] firstObject];
    }
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    self.selectIndex = indexPath;

    RVChangePlanCollectionViewCell *cell = (RVChangePlanCollectionViewCell*) [collectionView cellForItemAtIndexPath:indexPath];
    self.bookPath = [self.bookArr[indexPath.row] Meta_Key];
    self.bookName = cell.bookNameLabel.text;
    self.bookWordNum = cell.wordNumLabel.text;
   [self.collectionView reloadData];
    
}




@end
